<?php

namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Request;

class Hooks extends Adminbase
{
    protected $add_edit_field;
    public function initialize(){
        parent::initialize();
        $this->add_edit_field = [
            ['field'=>'name','title'=>'钩子名称','type'=>'text','verify'=>'required','tips'=>'唯一不可重复,大小写字母加“_”符号','order_by'=>1],
            ['field'=>'description','title'=>'钩子描述','type'=>'textarea','tips'=>'为钩子描述相关作用','order_by'=>1],
            ['field'=>'type','title'=>'钩子类型','type'=>'radio','tips'=>'钩子的作用点','data'=>['1'=>'页面视图','2'=>'控制器'],'value'=>'1'],
            ['field'=>'addons','title'=>'挂载的插件','type'=>'textarea','tips'=>'当前钩子挂载的插件,插件正常安装自动载入，无需手动编写','disabled'=>'disabled'],
            ['field'=>'status','title'=>'钩子状态','type'=>'radio','tips'=>'钩子是否可用','data'=>['1'=>'正常','2'=>'封禁'],'value'=>'1']
        ];
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $info = db('hooks')->order('id asc')->select();
        $filed = [
            ['field'=>'id','title'=>'ID'],
            ['field'=>'name','title'=>'钩子名称'],
            ['field'=>'description','title'=>'钩子描述'],
            ['field'=>'type','title'=>'钩子类型'],
            ['field'=>'addons','title'=>'挂载的插件'],
            ['field'=>'status','title'=>'钩子状态'],
            ['field'=>'create_at','title'=>'添加时间'],
            ['field'=>'update_at','title'=>'更新时间'],
        ];
        return view('public/list',['filed'=>$filed,'info'=>json_encode($info,JSON_UNESCAPED_UNICODE)]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function save()
    {
        return view('public/add',['filed'=>$this->add_edit_field]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $data = $request->param();
        if(!preg_match('/^[a-zA-Z_]+$/', $data['name'])){
            return json(['code'=>1,'msg'=>'钩子名称只能是 大小写字母 + “_”']);
        }
        $verify = add_edit_verify($data,['name'],['name'],'hooks');
        if($verify['code'] != 200){
            return json($verify);
        }
        $res = $this->add_info('hooks',$data);
        if($res['code'] == 200){
            session('mor_hooks',null);
        }
        return json($res);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return json(['code'=>1,'msg'=>'数据错误，need ID']);
        }
        $data = db('hooks')->find($id);
        if(!$data){
            return json(['code'=>1,'msg'=>'数据不存在']);
        }
        return view('public/edit',['filed'=>$this->add_edit_field,'info'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $data = $request->param();
        $verify = add_edit_verify($data,['id','name'],['name'],'hooks');
        if($verify['code'] != 200){
            return json($verify);
        }
        if(!preg_match('/^[a-zA-Z_]+$/', $data['name'])){
            return json(['code'=>1,'msg'=>'钩子名称只能是 大小写字母 + “_”']);
        }
        $res = $this->update_info('hooks',$data);
        if($res['code'] == 200){
            session('mor_hooks',null);
        }
        return json($res);
    }

    /**
     * 删除指定资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        if(!is_array($id)){
            if(!is_numeric($id)){
                return json(['code'=>1,'msg'=>'数据错误']);
            }else{
                $id = explode(',',$id);
            }
        }
        if(is_array($id)){
            foreach($id as $k=>$val){
                $old = db('hooks')->where(['id'=>$val])->find();
                if(!$old){
                    return json(['code'=>1,'msg'=>'存在非数据库数据，请核查']);
                }
            }
            $res = db('hooks')->delete($id);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据错误']);
        }
    }
}
